package com.exam.demo.service.impl;

import com.exam.demo.mapper.AnswerMapper;
import com.exam.demo.mapper.ProblemMapper;
import com.exam.demo.pojo.Answer;
import com.exam.demo.pojo.AnswerExample;
import com.exam.demo.pojo.Problem;
import com.exam.demo.pojo.ProblemExample;
import com.exam.demo.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LibraryImpl implements LibraryService {
    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private ProblemMapper problemMapper;

    @Override
    @Transactional
    //增加题目,成功返回1，否则返回0
    public int addProblem(Problem problem) {
        return this.problemMapper.insert(problem);
    }

    @Override
    @Transactional
    //删除题目,成功返回1，否则返回0
    public int deleteProblem(int proNo) {
        return this.problemMapper.deleteByPrimaryKey(proNo);
    }

    @Override
    @Transactional
    //修改题目,成功返回1，否则返回0
    public int editProblem(Problem problem) {
        return this.problemMapper.updateByPrimaryKeySelective(problem);
    }

    @Override
    @Transactional
    //新增答案,成功返回1，否则返回0
    public int addAnswer(Answer answer) {
        return this.answerMapper.insert(answer);
    }

    @Override
    @Transactional
    //删除答案,成功返回1，否则返回0
    public int deleteAnswer(int ansNo) {
        return this.answerMapper.deleteByPrimaryKey(ansNo);
    }

    @Override
    @Transactional
    //修改答案,成功返回1，否则返回0
    public int editAnswer(Answer answer) {
        return this.answerMapper.updateByPrimaryKeySelective(answer);
    }

    @Override
    @Transactional
    //新增答案后更新问题的外键
    public int updateProblemAdd(Answer answer) {
        AnswerExample answerExample = new AnswerExample();
        Problem problem = new Problem();
        AnswerExample.Criteria criteria = answerExample.createCriteria();
        criteria.andProNoEqualTo(answer.getProNo());
        List<Answer> answers = this.answerMapper.selectByExample(answerExample);
        for(Answer answer1 : answers){
            problem = this.problemMapper.selectByPrimaryKey(answer1.getProNo());
            problem.setProAnsno(answer1.getAnsNo());
            this.problemMapper.updateByPrimaryKeySelective(problem);
        }
        return 1;
    }

    @Override
    @Transactional
    //删除答案后更新问题的外键
    public int updateProblemDelete(int ansNo) {
        Answer answer = this.answerMapper.selectByPrimaryKey(ansNo);
        Problem problem = this.problemMapper.selectByPrimaryKey(answer.getProNo());
        problem.setProAnsno(null);
        return this.problemMapper.updateByPrimaryKey(problem);
    }

    @Override
    public Problem getProByProNo(int proNo) {
        return this.problemMapper.selectByPrimaryKey(proNo);
    }

    @Override
    public List<Problem> getProblemList() {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andProNoIsNotNull();
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
    //通过teaNo去查找对应题目，返回List<Problem>
    public List<Problem> getProblemListByTeaNo(String teaNo) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andTeaNoEqualTo(teaNo);
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
    //通过proDetail去查找对应题目，返回List<Problem>
    public List<Problem> getProblemListByProDetail(String proDetail) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andProDetailLike("%"+proDetail+"%");
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
    //通过proKeyw去查找对应题目，返回List<Problem>
    public List<Problem> getProblemListByProKeyw(String proKeyw) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andProKeywLike("%"+proKeyw+"%");
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
    //通过proType去查找对应题目，返回List<Problem>
    public List<Problem> getProblemListByProType(String proType) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andProTypeEqualTo(proType);
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
    //通过chaNo去查找对应题目，返回List<Problem>
    public List<Problem> getProblemListByChaNo(int chaNo) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andChaNoEqualTo(chaNo);
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
    //获取改题目对应的答案
    public Answer getAnswerByProAnsno(int proAnsno) {
        return this.answerMapper.selectByPrimaryKey(proAnsno);
    }

    @Override
    public int getAnsnoByProNo(int proNo) {
        return this.problemMapper.selectByPrimaryKey(proNo).getProAnsno();
    }

    @Override
	@Transactional
	//通过难度获取题目
	public List<Problem> getProblemListByProDif(String proDif) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andProDifEqualTo(proDif);
        return this.problemMapper.selectByExample(problemExample);
    }

    @Override
    @Transactional
	//通过难度和类型获取题目
    public List<Problem> getProblemListByProDifAndProType(String proDif, String proType) {
        ProblemExample problemExample = new ProblemExample();
        ProblemExample.Criteria criteria = problemExample.createCriteria();
        criteria.andProDifEqualTo(proDif);
        criteria.andProTypeEqualTo(proType);
        return this.problemMapper.selectByExample(problemExample);
    }
}
